program qrw_square_lattice_coin_model
 use parametros
 use coeficientes
 use nome_arq
 use probabilidades
 implicit none
!-----------------------------------------------------------------------
!Amplitude de probabilidade de encontrara particula em um dado vertice.
 complex*16, dimension(1:4,-dm:dm,-dm:dm) :: ket,ket0
!-----------------------------------------------------------------------
!Variaveis Auxiliares
 integer :: i,l,t,w,z,s,j,k
 real*8 :: soma,pmax
!-----------------------------------------------------------------------
!Estado Inicial do sistema
 ket0(1,0,0)=(1.0d0,0.0d0)/2.0d0
 ket0(2,0,0)=(0.0d0,1.0d0)/2.0d0
 ket0(3,0,0)=(1.0d0,0.0d0)/2.0d0
 ket0(4,0,0)=(0.0d0,1.0d0)/2.0d0
!-----------------------------------------------------------------------
 do w=0,step
!-----------------------------------------------------------------------
!Evolucao de uma etapa
  if(w > 0)then
   do j=-dm,dm
    do k=-dm,dm
     do s=1,4
      if(ket0(s,j,k) .NE. (0.0,0.0)) then
       ket(1,j+1,k)=ket(1,j+1,k)+coef(1,s,j,k)*ket0(s,j,k)
       ket(2,j-1,k)=ket(2,j-1,k)+coef(2,s,j,k)*ket0(s,j,k)
       ket(3,j,k+1)=ket(3,j,k+1)+coef(3,s,j,k)*ket0(s,j,k)
       ket(4,j,k-1)=ket(4,j,k-1)+coef(4,s,j,k)*ket0(s,j,k)
      endif
     enddo
    enddo
   enddo
   ket0=ket
   ket=(0.0,0.0)
  endif
!------------------------------------------------------------------------- 
  do j=-dm,dm
   do k=-dm,dm
    if(prob(ket0,j,k) .NE. 0.0) then
     if(pmax <= prob(ket0,j,k))then
      pmax=prob(ket0,j,k)
     endif
     soma=soma+prob(ket0,j,k)
    endif
   enddo
  enddo
!-------------------------------------------------------------------------
  do j=-dm,dm
   do k=-dm,dm
    if(prob(ket0,j,k) .NE. 0.0) then
     open(10,file="dados/dados2D"//nome(w)//".dat")
     write(10,*)j,k,prob(ket0,j,k)/pmax
    endif
    open(11,file="dados/dados3D"//nome(w)//".dat")
    write(11,*)j,k,prob(ket0,j,k)/pmax
   enddo
  enddo
!-------------------------------------------------------------------------
  close(10)
  close(11)
  print*,w,soma
  soma=0.0d0
  pmax=0.0d0
 enddo
end program qrw_square_lattice_coin_mode